package com.shanjupay.common.utilbak;

import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * 卡号转换工具类
 */
public class CardNoUtil {

    /**
     * 位置替换  ABCDEF -> EFCDAB
     * @param cardNo
     * @return
     */
    public static String positionReplace(String cardNo){
        if(StringUtils.isBlank(cardNo)){
            return cardNo;
        }
        String toCardNo = "";

        int size = cardNo.length()/2;

        if(size*2 == cardNo.length()){
           for(int i = cardNo.length()/2; i > 0;i--){
                toCardNo += cardNo.substring((i-1)*2,i*2);
            }
        }else {
            for(int i = cardNo.length()/2 + 1; i > 0;i--){
                toCardNo += cardNo.substring( i - 1  == 0 ? 0 : (i-1)*2 -1  ,i*2 - 1);
            }
        }
        return toCardNo;
    }

    /**
     * 16进制转10进制  补零
     * @param cardNo
     * @return
     */
    public static String sixteenToTen(String cardNo){
        if(StringUtils.isBlank(cardNo)){
            return cardNo;
        }
        String toCardNo = "";
        try {
            toCardNo = new BigInteger(cardNo, 16).toString(10);
        }catch (Exception e){
            e.printStackTrace();
        }
        toCardNo = String.format("%010d", new BigDecimal(toCardNo).longValue());
        return toCardNo;
    }


    /**
     * 16进制转10进制  不补零
     * @param cardNo
     * @return
     */
    public static String sixteenToTen2(String cardNo){
        if(StringUtils.isBlank(cardNo)){
            return cardNo;
        }
        String toCardNo = "";
        try {
            toCardNo = new BigInteger(cardNo, 16).toString(10);
        }catch (Exception e){
            e.printStackTrace();
        }
//        toCardNo = String.format("%010d", new BigDecimal(toCardNo).longValue());
        return toCardNo;
    }


    public static boolean isTenNumber(String str) {
        boolean flag = false;
        for(int i=0;i<str.length();i++){
        char c = str.charAt(i);
        if(c=='0'|c=='1'|c=='2'|c=='3'|c=='4'|c=='5'|c=='6'|c=='7'|c=='8'|c=='9'){
            flag =true;
        }else {
            flag = false;
            break;
        }
    }
     return flag;

}



    public static boolean isHexNumber(String str){
        boolean flag = false;
        for(int i=0;i<str.length();i++){
            char cc = str.charAt(i);
            if(cc=='0'||cc=='1'||cc=='2'||cc=='3'||cc=='4'||cc=='5'||cc=='6'||cc=='7'||cc=='8'||cc=='9'||cc=='A'||cc=='B'||cc=='C'||
                    cc=='D'||cc=='E'||cc=='F'||cc=='a'||cc=='b'||cc=='c'||cc=='d'||cc=='e'||cc=='f'){
                flag = true;
            }else {
                flag = false;
                break;
            }
       }
       return flag;

}


    public static void main(String[] args) {
//        System.err.println(positionReplace("AB123456"));
        System.err.println(sixteenToTen2("A22"));

    }
}
